package sonet;
import java.util.*;

/** Class Agent. */

public abstract class Agent implements IdentificableEntity {
	
	private int _identifier;
	private String _name;
    private String _phone;
	private String _email;
	private Permission _accessAgentProfile = new Permission();
	private Permission _accessPublication = new Permission();
	private Permission _connectionPermission = new Permission();
	private Permission _mailingPermission = new Permission();
	private List<Publication> _listPublication = new ArrayList<Publication>();	
	private List<Message> _inbox = new ArrayList<Message>();
	private List<Message> _outbox = new ArrayList<Message>();
	private List<Connection> _listConnection = new ArrayList<Connection>();
	private AgentType _type;
	private AgentStatus _status = AgentStatus.ACTIVE;
	
/** Agent's Constructor. */	
	public Agent(int id, String name, String email, String phone, AgentType type){
		_identifier = id;
		_name = name;
		_phone = phone;
		_email = email;
		_type = type;
	}
/** Gets and Sets of the Agent's Attributes. */
	public AgentType getType(){ return _type; }
	public AgentStatus getAgentStatus(){ return _status;}
	public int getIdentifier(){return _identifier;}
	void setName(String name){_name = name;}
	public String getName(){return _name;}
	void setPhone(String phone){_phone = phone;}
	public String getPhone(){return _phone;}
	void setEmail(String email){_email = email;}
	public String getEmail(){return _email;}
	public Collection<Publication> getPublications(){return _listPublication;}
	public Collection<Message> getInbox(){ return _inbox;}
	public Collection<Message> getOutbox(){ return _outbox;}
	public Collection<Connection> getConnections(){ return _listConnection;}
	public void messageReceived(Message m){	_inbox.add(m);}	
	public void messageSent(Message m){ _outbox.add(m);}
	
/** Equals method for Agent. */
	@Override 
	public boolean equals(Object o){
		if(o == null) return false;
		else{
			if( o instanceof Agent ) {
				Agent a = (Agent)o;
				return (_identifier == a.getIdentifier());
			}
			else return false;
			
		}
	}	
/** Activates an Agente. */	
	public void activate(){}
	
/** Deactivates an Agente. */	
	public void desactivate(){}
	
/** Protects the Agent profile, not allowing other Agents to access it. */ 
	public void protectProfile(Agent a){
		_accessAgentProfile.protect(a);
	}
	
/** Allows acess to the Agent's Profile. */
	public void unprotectProfile(Agent a){
		_accessAgentProfile.unProtect(a);
	}
	
/** Checks if has permission to see one Agent's Profile. */ 	
	public boolean hasPermissionToViewProfileAgent(Agent a){
		return _accessAgentProfile.hasAccess(a);
	}
	

/** Adds one Publication to the Agent's Publications List. */	
	public void registPublication(Publication pb){
		_listPublication.add(pb);
	}
	
/** Creates a comment (using the method comment from PublicationComponent) and adds it to the list of comments of the designed Publication. */ 	
	public void comment(Publication pb, String text){
		pb.comment(text);
	}
	
/** Establishes the Connection and adds to the Agent's Connections List. */
	public void connectionEstablished(Connection c){
		_listConnection.add(c);
	}
	
/** Allows the creation of a Connection with an Agent. */	
	public void allowConnection(Agent a){
		_connectionPermission.unProtect(a);
	}

/** Blocks the creation of a Connection with an Agent. */	
	public void blockConnection(Agent a){
		_connectionPermission.protect(a);
	}
	
/** Checks if has permission to establish a connection with a certain Agent. */
	public boolean CanEstablishConnection(Agent a){
		return _connectionPermission.hasAccess(a);
	}
}